package com.example.secondhandsysapi.Controller;


import com.example.secondhandsysapi.Entity.Category;
import com.example.secondhandsysapi.Service.CategoryService;
import com.example.secondhandsysapi.Vo.*;
import com.example.secondhandsysapi.util.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.sql.Date;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@CrossOrigin
@RestController
@RequestMapping("category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @GetMapping("all")
    public List<Category> getAllCategoryInfo(){
        return categoryService.getAllCategoryInfo();
    }

    @GetMapping("/goodsinfo")
    public List<CategoryGoodsInfo> getGoodsInfo(@RequestParam Integer id){
        return categoryService.getAllCategoryGoods(id);
    }

    @GetMapping("/info")
    public List<Category> getAllCategory(@RequestParam(required = false) String name) {
        List<Category> categoryinfo = null;
        if(name != null){
            // 当商品类别名称不为空时，根据类别名称查询商品类别信息
            categoryinfo = categoryService.getAllCategory(name);
        }else {
            // 当商品类别为空时，返回所有商品类别信息
            categoryinfo = categoryService.getAllCategoryInfo();
        }
        return categoryinfo;
    }

    @PostMapping("/insert")
    public Integer insertCategoryInfo(@RequestBody Category category) {
        Integer i = categoryService.insertCategory(category);
        return i;
    }

    @GetMapping("/del/{id}")
    private Integer delInfoById(@PathVariable Integer id) {
        return categoryService.delById(id);
    }

    @PostMapping("/update")
    public Integer updateCategoryInfo(@RequestBody Category category) {
        Integer i = categoryService.updateCategory(category);
        return i;
    }

    //http://localhost:8080/category/upload
    @PostMapping("/upload")
    public String getImageFileName(MultipartFile file) {
        String oldFileName = file.getOriginalFilename();
        System.out.println(oldFileName);
        String typeName = oldFileName.substring(oldFileName.lastIndexOf("."));
        String filePath = FileUtil.getUpLoadFilePath();
        System.out.println(typeName);
        String newFileName = System.currentTimeMillis() + typeName;

        try {
            FileUtil.uploadFile(file.getBytes(),filePath,newFileName);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        return newFileName;
    }

    //前端商品系列
    @GetMapping("/querycate")
        public List<Category> queryCategory(@RequestParam(required = false) Integer category_id) {
        return categoryService.getCategoryList(category_id);
    }

    //获取ip的金额和销量
    // http://localhost:8080/category/ipMoneyandcount
    @PostMapping("ipMoneyandcount")
    public IpMoney ipMoneyAndCount() {
    List<Map<String, Object>> resultList = categoryService.getIpMoney();

    IpMoney ipMoney = new IpMoney();
    List<String> categoryNames = new ArrayList<>();
    List<Double> totalSales = new ArrayList<>();
    List<Long> salesCounts = new ArrayList<>();

        for (Map<String, Object> result : resultList) {
        categoryNames.add((String) result.get("category_name"));
        totalSales.add((Double) result.get("total_sales"));
        salesCounts.add((Long) result.get("sales_count"));
    }

        ipMoney.setCategory_name(categoryNames);
        ipMoney.setTotal_sales(totalSales);
        ipMoney.setSales_count(salesCounts);

        return ipMoney;
}


    // 分类饼图
    // http://localhost:8080/category/IpRatio
    @PostMapping("IpRatio")
    public CategoryCount getIpRatio() {
        List<CGC> categoryGoods = categoryService.getCategoryGoods();
        if (categoryGoods == null) {
            throw new IllegalStateException("获取分类统计失败"); // 处理genderCounts为null的情况，例如返回一个默认的GenderCount对象或者抛出异常
        }
        CategoryCount ret = new CategoryCount();
        List<String> key = new ArrayList<>();
        List<Integer> vl = new ArrayList<>();
        for (CGC cgc : categoryGoods) {
            key.add(cgc.getCategory_name());
            vl.add(cgc.getGoods_count());
        }
        ret.setCategory_names(key);
        ret.setGoods_counts(vl);
        return ret;
    }

}
